package com.fg.cloud.factory.controller.agency;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.AgencyBill;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.agency.AgencyBillEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyBillVo;
import com.fg.cloud.manager.service.agency.AgencyBillService;

/**
 * Description:经销商账单流水controller
 * 
 * @author shaoqiping
 * @date 2017年8月21日上午11:20:41
 */
@Controller
@RequestMapping("/agencyBill")
public class AgencyBillController extends BaseController {

	@Autowired
	AgencyBillService billService;

	/**
	 * Description:新增对账单还款流水
	 * 
	 * @param request
	 * @param response
	 * @param entity
	 * @author shaoqiping
	 * @date 2017年8月24日下午5:58:44
	 */
	@RequestMapping(value = "/insertRepayment", method = { RequestMethod.POST })
	@ResponseBody
	public void insertRepayment(HttpServletRequest request, HttpServletResponse response, AgencyBillEntity entity) {
		try {
			if (entity == null || LongUtils.isEmpty(entity.getAgeId())) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			entity.setFacId(getCurrentFactoryUser().getFacId());
			entity.setTransTime(new Date());
			entity.setConfirmTime(new Date());
			entity.setTransInfo(4);
			entity.setStatus(1);
			boolean flag = billService.insert(entity,true);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:查询指定经销商账户流水
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param filterText
	 * @param startTime
	 * @param endTime
	 * @param entity
	 * @author shaoqiping
	 * @date 2017年8月21日下午2:13:42
	 */
	@RequestMapping(value = "/findBills", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText, String startTime, String endTime, AgencyBillVo entity) {
		try {
			if(entity==null||LongUtils.isEmpty(entity.getAgeId())){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<AgencyBillVo> pageinfo = new PageUtils<AgencyBillVo>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			condition.put("ageId", entity.getAgeId());
			condition.put("startTime", startTime);
			condition.put("endTime", endTime);
			condition.put("status", entity.getStatus());
			condition.put("transInfo", entity.getTransInfo());
			condition.put("flag", AgencyBill.FAC);
			condition.put("flag2", !AgencyBill.ISORDER);
			condition.put("valueAdd", AgencyBill.VALUE_ADD);
			pageinfo.setCondition(condition);

			billService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:查询订单付款详情
	  * @param request
	  * @param response
	  * @param orderId
	  * @author shaoqiping
	  * @date 2017年9月14日下午3:06:33
	*/
	@RequestMapping(value = "/findOrderAmount", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void findOrderAmount(HttpServletRequest request, HttpServletResponse response,Integer page, Integer rows,
			String sort, String order,Long orderId){
		try {
			if(orderId==null||LongUtils.isEmpty(orderId)){
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应订单");
				return;
			}
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<AgencyBillEntity> pageinfo = new PageUtils<AgencyBillEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("id", orderId);
			pageinfo.setCondition(condition);

			billService.findOrderAmount(pageinfo);
			
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:订单流水高级查询
	  * @param request
	  * @param response
	  * @param page
	  * @param rows
	  * @param sort
	  * @param order
	  * @param filterText
	  * @param startTime
	  * @param endTime
	  * @param entity
	  * @author shaoqiping
	  * @date 2017年8月31日上午10:16:02
	*/
	@RequestMapping(value = "/findAllOrders", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void findAllOrders(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText, String startTime, String endTime,AgencyBillVo entity) {
		try {
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<AgencyBillVo> pageinfo = new PageUtils<AgencyBillVo>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("ageId", entity.getAgeId());
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			condition.put("startTime", startTime);
			condition.put("endTime", endTime);
			condition.put("status", entity.getStatus());
			condition.put("transInfo", entity.getTransInfo());
			condition.put("account", entity.getCollectionAccount());
			if(entity.getAreaNum()!=null){
				String[] str=entity.getAreaNum().split("[,]");
				condition.put("areaId", str[str.length-1]);
				StringBuffer temp=new StringBuffer();
				for (int i = 0; i < str.length-1; i++) {
					if(i==str.length-2){
						temp.append(str[i]);
					}else{
						temp.append(str[i]+"_");
					}
				}
				condition.put("areaNumber", temp);
			}
			condition.put("levelId", entity.getLevelId());
			condition.put("flag", AgencyBill.FAC);
			condition.put("flag2", AgencyBill.ISORDER);
			condition.put("valueAdd", AgencyBill.VALUE_ADD);
			pageinfo.setCondition(condition);

			billService.highFindList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
